<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="keywords" content="">
<meta name="description" content="">
<title>用户管理</title>
<link href="../../web-frame/layui/css/layui.css" rel="stylesheet"/>
<link href="../../web-frame/ruoyi/css/bootstrap.min.css" rel="stylesheet"/>
<link href="../../web-frame/ruoyi/css/font-awesome.min.css" rel="stylesheet"/>
<!-- bootstrap-table 表格插件样式 -->
<link href="../../web-frame/ruoyi/libs/bootstrap-table/bootstrap-table.min.css?v=20201129" rel="stylesheet"/>
<link href="../../web-frame/ruoyi/css/animate.css" rel="stylesheet"/>
<link href="../../web-frame/ruoyi/css/style.css?v=20200903" rel="stylesheet"/>
<link href="../../web-frame/ruoyi/ruoyi/css/ry-ui.css?v=4.6.0" rel="stylesheet"/>
<script src="../../web-frame/ruoyi/js/jquery.min.js"></script>
<script src="../../web-frame/ruoyi/js/bootstrap.min.js"></script>
<!-- bootstrap-table 表格插件 -->
<script src="../../web-frame/ruoyi/libs/bootstrap-table/bootstrap-table.min.js?v=20201129"></script>
<script src="../../web-frame/ruoyi/libs/bootstrap-table/locale/bootstrap-table-zh-CN.min.js?v=20201129"></script>
<script src="../../web-frame/ruoyi/libs/bootstrap-table/extensions/mobile/bootstrap-table-mobile.min.js?v=20201129"></script>
<!-- jquery-validate 表单验证插件 -->
<script src="../../web-frame/ruoyi/libs/validate/jquery.validate.min.js"></script>
<script src="../../web-frame/ruoyi/libs/validate/messages_zh.min.js"></script>
<script src="../../web-frame/ruoyi/libs/validate/jquery.validate.extend.js"></script>
<!-- jquery-validate 表单树插件 -->
<script src="../../web-frame/ruoyi/libs/bootstrap-treetable/bootstrap-treetable.js"></script>
<!-- 遮罩层 -->
<script src="../../web-frame/ruoyi/libs/blockUI/jquery.blockUI.js"></script>
<script src="../../web-frame/ruoyi/libs/iCheck/icheck.min.js"></script>
<script src="../../web-frame/ruoyi/libs/layer/layer.min.js"></script>
<script src="../../web-frame/layui/layui.js"></script>
<script src="../../web-frame/ruoyi/libs/layer/laytpl.js"></script>
<script src="../../web-frame/ruoyi/libs/layer/form.js"></script>
<script src="../../web-frame/ruoyi/ruoyi/js/common.js?v=4.6.0"></script>
<script src="../../web-frame/ruoyi/ruoyi/js/ry-ui.js?v=4.6.0"></script>
<script src="/web-frame/element/js/axios.js"></script>
<script src="/web-frame/js/glob.js"></script>
<body>

<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="role-form">
                <div class="select-list">
                    <ul>
                        <li>
                            关键字查询：<input type="text" name="keyword"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="addData()">
                <i class="fa fa-plus"></i> 新增菜单
            </a>
            <a class="btn btn-primary " onclick="editDataSelect()">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger " onclick="deleteData()">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="exportData()">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>

<!--   新增弹窗-->
<script id="add-from" type="text/html">
    <form class="layui-form layui-form-pane" id="form-add" action="">
        <div class="form-group">
            <div class="layui-form-item">
                <label class="layui-form-label">字段一</label>
                <div class="layui-input-block">
                    <input type="text" name="field"  autocomplete="off" placeholder="请输入字段一"  class="layui-input" >
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">字段二</label>
                <div class="layui-input-block">
                    <input type="text" name="field"  autocomplete="off" placeholder="请输入字段二"  class="layui-input" >
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">单行选择框</label>
                <div class="layui-input-block">
                    <select name="interest" lay-filter="aihao">

                        <!--    模板的foreach循环                    -->
                        {{#  layui.each(d.rows, function(index, item){ }}
                        <option value={{item.key}}>{{item.value}}</option>
                        {{#  }); }}

                        <!--  单纯的if                        -->
                        {{#  if(d.rows.length === 0){ }}
                        <option value=""> 无数据</option>
                        {{#  } }}

                    </select>
                </div>
            </div>
        </div>
    </form>
</script>

<!--修改弹窗-->
<script id="update" type="text/html">
    <div style="padding: 10px">
        <form class="layui-form layui-form-pane" id="update-form" action="">
            <input type="text" name="id" value={{d.id}} autocomplete="off" placeholder="隐藏的唯一id" hidden  class="layui-input" >
            <div class="layui-form-item">
                <label class="layui-form-label">字段一</label>
                <div class="layui-input-block">
                    <input type="text" name="field" value={{d.field}} autocomplete="off" placeholder="请输入字段一"  class="layui-input" >
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">字段二</label>
                <div class="layui-input-block">
                    <input type="text" name="field" value={{d.field}} autocomplete="off" placeholder="请输入字段二"  class="layui-input" >
                </div>
            </div>
        </form>
    </div>
</script>

<script>

    var urlData={
        page:'/layui/gettable',
        save:'url',
        update:'url',
        get:'url',
        delete:'url'
    }

    $(function() {
        var options = {
            url: urlData.page,
            modalName: "菜单列表",
            columns: [{
                checkbox: true
            },
                {
                    title: '序号',
                    align: 'center',
                    halign: 'center',
                    width: 40,
                    formatter: function (value, row, index) {
                        //获取每页显示的数量
                        var pageSize=$('#bootstrap-table').bootstrapTable('getOptions').pageSize;
                        //获取当前是第几页
                        var pageNumber=$('#bootstrap-table').bootstrapTable('getOptions').pageNumber;
                        //返回序号，注意index是从0开始的，所以要加上1
                        return pageSize * (pageNumber - 1) + index + 1;
                    }
                },
                {
                    field: 'key1',
                    title: '字段名称_1'
                },
                {
                    title: '字段名称_2',
                    align: 'center',
                    width: 100,
                    formatter: function (value, row, index) {
                        var value=row.key2
                        return '这是'+value;
                    }
                },
                {
                    title: '操作',
                    width: '20',
                    widthUnit: '%',
                    align: "left",
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info btn-xs " href="javascript:void(0)" onclick="editData(\'' + row.id + '\')">修改</a> ');
                        actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick="removeData(\'' + row.id + '\')">删除</a>');
                        return actions.join('');
                    }
                }
            ]
        };
        $.table.init(options);
    });



    //新增弹窗
    //新增弹窗
    var index_add
    function addData() {
        var data=urldata('post','/layui/getselectList',undefined)
        laytemplateSave_1(data,'add-from')
        index_add=layer.open({
            content: $('#add-from').html()
            ,area: ['500px', '400'] //自定义文本域宽高
            ,title:'xx新增'
            ,btn: ['确定', '取消']
            ,yes: function(){
                submitEnter();
            }
            ,btn2: function(){
                //按钮【取消】的回调
                return 0;
            } //用于layui表单重新渲染
            ,success: function(layero){
                layui.use('form', function(){
                    var form = layui.form;
                    form.render();
                });
            }
        });
    }
    //对应上面的
    var num_2=0
    var tplContent_save_1;
    function laytemplateSave_1(obj,id) {
        var laytpl = layui.laytpl;
        var id=document.getElementById(id);
        if(num_2===0){
            tplContent_save_1=id.innerHTML;
            num_2++;
        }
        var tpl=tplContent_save_1;
        laytpl(tpl).render(obj, function(html){
            id.innerHTML = html;
        });
    }


    //确认
    function submitEnter() {
        var data= $("#form-add").serialize();
        data = decodeURIComponent(data,true);
        //会将弹窗的表单数据封装成json数据
        data=DataDeal.formToJson(data);
        var data=urldata('post',urlData.save,data);
        if(data.code==0){
            layer.msg("添加成功");
            //关闭弹窗
            layer.close(index_add);
            $('#bootstrap-table').bootstrapTable(('refresh'));
        }
    }




    var update_index
    //修改数据
    function editData(obj) {
        var data=urldata('post',urlData.get+'/'+obj,undefined)
        laytemplateUpdate_1(data.rows,'update')
        update_index=layer.open({
            content: $('#update').html()
            ,area: ['600px', '450px'] //自定义文本域宽高
            ,title:'用户信息修改'
            ,type: 1
            ,btn: ['确定', '取消']
            ,yes: function(){
                submitupdate(obj);
            }
            ,btn2: function(){
                //按钮【取消】的回调
                return 0;
            }
            //用于layui表单重新渲染
            ,success: function(layero){
                layui.use('form', function(){
                    var form = layui.form;
                    form.render();
                });
            }
        });
    }
    //对应上面的
    var num_1=0
    var tplContent_update_1;
    function laytemplateUpdate_1(obj,id) {
        var laytpl = layui.laytpl;
        var id=document.getElementById(id);
        if(num_1===0){
            tplContent_update_1=id.innerHTML;
            num_1++;
        }
        var tpl=tplContent_update_1;
        laytpl(tpl).render(obj, function(html){
            id.innerHTML = html;
        });
    }
    function submitupdate(obj) {
        var data= $("#update-form").serialize();
        data = decodeURIComponent(data,true);
        data=DataDeal.formToJson(data);
        var obj=urldata('post',urlData.update,data);
        if(obj.code==0){
            layer.msg("修改成功")
            //刷新表单
            $('#bootstrap-table').bootstrapTable(('refresh'));
            layer.close(update_index);
        }
    }


    function editDataSelect() {
        var obj= $("#bootstrap-table").bootstrapTable('getSelections');
        if(obj===''||obj===undefined){
            return layer.msg("请先选择一行数据");
        }
        if(obj.length>1){
            return layer.msg("只能选择一行数据");
        }
        layer.msg("要修改的数据"+JSON.stringify(obj));
        editData(obj.id);
    }


    function deleteData() {
        var obj= $("#bootstrap-table").bootstrapTable('getSelections');
        if(obj===''||obj===undefined){
            return layer.msg("请先选择一行数据");
        }
        if(obj.length>1){
            return layer.msg("只能选择一行数据");
        }
        layer.msg("要删除的数据"+JSON.stringify(obj))
        removeData(obj.id)
    }

    function removeData(data) {
        var obj=urldata('post',urlData.delete+'/'+obj.id,data);
        if(obj.code==0){
            layer.msg("删除成功")
            //刷新表单
            $('#bootstrap-table').bootstrapTable(('refresh'));
        }
    }


    var DataDeal = {
        //将从form中通过$('#refer').serialize()获取的值转成json
        formToJson: function (data) {
            data=data.replace(/&/g,"\",\"");
            data=data.replace(/=/g,"\":\"");
            data="{\""+data+"\"}";
            return data;
        }
    };

</script>
</body>
</html>